package com.jiuzhi.logger.util;/**
 * Created with IDEA
 * author:tangsixiang@163.com
 * Date:2018/11/30
 * Time:11:36
 */

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiuzhi.logger.pojo.NginxDemain;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;
import scala.Int;

import java.util.HashMap;
import java.util.Map;

/****
 @ClassName NginxStrSplitter
 @Description TODO
 @Author tangsixiang
 @create: 2018-11-30 11:36
 **/
@JsonIgnoreProperties(ignoreUnknown = true)
public class NginxStrSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {

    @Override
    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
        String tmp = "{\"timestamp\":\"16/May/2019:20:32:09 +0800\",\"remote_addr\":\"47.105.77.192\",\"referer\":\"-\",\"request\":\"POST /carServer/receive/stationStatus/1.0 HTTP/1.1\",\"status\":200,\"bytes\":12,\"agent\":\"-\",\"forwarded\":\"-\",\"up_addr\":\"172.16.6.17:8116\",\"up_host\":\"-\",\"up_resp_time\":\"0.003\",\"request_time\":\"0.039\"}";

        try {
            //把数据转换为对象
            NginxDemain nginxDemain = new ObjectMapper().readValue(value.replaceAll(" ", "").replaceAll("\n", "").replaceAll("\r", ""), NginxDemain.class);
            //请求字符串二次处理
            //继续向下处理
            out.collect(new Tuple2<String, Integer>(changeReqeustValue(nginxDemain.getRequest()), 1));

        } catch (Exception e) {
            e.printStackTrace();
            out.collect(new Tuple2<String, Integer>("not-pathmethod", 1));
        }


    }


    public String changeReqeustValue(String request) {

        try {
            //字符串业务解析操作  截取中间的字符串 去掉 有？参数的模块
            String[] arry = request.split(" ");
            String newReq = arry[1];
            if (newReq.indexOf("?") >= 1) {
                newReq = newReq.split("\\?")[0];
            }
            return newReq;

        } catch (Exception e) {

            return "not-pathmethod";

        }


    }

}
